package com.nidu.demo.tenant.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.tenant.convertor.TenantPackageConvertor;
import com.nidu.demo.tenant.gateway.TenantPackageGateway;
import com.nidu.demo.tenant.mapper.TenantPackageMapper;
import com.nidu.demo.tenant.mapper.dataobject.TenantPackageDO;
import com.nidu.demo.tenant.model.TenantPackage;
import com.nidu.demo.tenant.model.TenantPackageCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

@Component
@RequiredArgsConstructor
public class TenantPackageGatewayImpl implements TenantPackageGateway {

    private final TenantPackageMapper mapper;
    private final TenantPackageConvertor convertor;

    @Override
    public TenantPackage create(TenantPackage tenantPackage) {
        TenantPackageDO tenantPackageDO = convertor.toDataObject(tenantPackage);
        mapper.insert(tenantPackageDO);
        return convertor.toDomainObject(tenantPackageDO);
    }

    @Override
    public TenantPackage update(TenantPackage tenantPackage) {
        TenantPackageDO tenantPackageDO = convertor.toDataObject(tenantPackage);
        mapper.updateById(tenantPackageDO);
        return convertor.toDomainObject(tenantPackageDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteBatchIds(ids);
    }

    @Override
    public TenantPackage getById(Long id) {
        TenantPackageDO tenantPackageDO = mapper.selectById(id);
        if(tenantPackageDO == null){
            return null;
        }
        return convertor.toDomainObject(tenantPackageDO);
    }

    @Override
    public List<TenantPackage> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<TenantPackageDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TenantPackageDO::getId, ids);
        List<TenantPackageDO> tenantPackageDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(tenantPackageDOS)){
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(tenantPackageDOS);
    }

    @Override
    public TenantPackage getByName(String name) {
        TenantPackageDO tenantPackageDO = mapper.selectByName(name);
        if(tenantPackageDO == null){
            return null;
        }
        return convertor.toDomainObject(tenantPackageDO);
    }

    @Override
    public List<TenantPackage> listByCondition(TenantPackageCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<TenantPackageDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, TenantPackageDO::getStatus, criteria.getStatus());
        wrapper.in(CollUtil.isNotEmpty(criteria.getTenantPackageIds()), TenantPackageDO::getId, criteria.getTenantPackageIds());

        wrapper.like(!StrUtil.isEmpty(criteria.getName()), TenantPackageDO::getName, criteria.getName());

        // 执行查询
        List<TenantPackageDO> tenantPackageDOS = mapper.selectList(wrapper);
        return convertor.toDomainObjects(tenantPackageDOS);
    }

    @Override
    public PageResponse<TenantPackage> pageByCondition(TenantPackageCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<TenantPackageDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, TenantPackageDO::getStatus, criteria.getStatus());
        wrapper.in(CollUtil.isNotEmpty(criteria.getTenantPackageIds()), TenantPackageDO::getId, criteria.getTenantPackageIds());

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(TenantPackageDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(TenantPackageDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        wrapper.like(!StrUtil.isEmpty(criteria.getName()), TenantPackageDO::getName, criteria.getName());

        // 创建分页对象
        Page<TenantPackageDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        } else {
            // 默认按排序号升序
            page.addOrder(OrderItem.asc("sort_order"));
        }

        // 执行分页查询
        mapper.selectPage(page, wrapper);
        List<TenantPackage> tenantPackages = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(tenantPackages, (int)page.getTotal(), (int)page.getSize(), (int)page.getCurrent());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        TenantPackageDO tenantPackageDO = new TenantPackageDO();
        tenantPackageDO.setId(id);
        tenantPackageDO.setStatus(status);
        mapper.updateById(tenantPackageDO);
    }
}